package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.User;
import com.itheima.health.service.SetMealService;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author spt
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {


    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private UserService userService;


    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}", setmeal, checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(), checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(), page.getResult());
    }



    /**
     * 查询所有套餐
     *
     * @return
     */
    @Override
    public List<Setmeal> list() {
        List<Setmeal> setmeals = setMealDao.selectList();
        return setmeals;
    }


    @Override
    public Setmeal getSetmealById(Integer setmealId) {
        Setmeal setmeal = setMealDao.selectOneById(setmealId);
        return setmeal;
    }

    /**
     * 根据id获取指定套餐信息
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        //获取套餐的基本信息
        Setmeal setmeal = setMealDao.selectOneById(id);
        //获取套餐关联的检查组id集合
        List<Integer> checkGroupIds = setMealDao.selectCheckgroupIdsBySetmealId(id);
        //该套餐有关联的检查组
        if(checkGroupIds !=null){
            List<CheckGroup> checkGroups = new ArrayList<>();
            for (Integer checkGroupId : checkGroupIds) {
                //根据检查组id查询该检查组信息
                CheckGroup checkGroup = checkGroupDao.selectById(checkGroupId);
                //根据检查组id查询关联检查项id集合
                List<Integer> checkItemIds  = checkGroupDao.selectCheckItemIdsByCheckGroupId(checkGroupId);
                //该检查组有关联的检查项
                if(checkItemIds!=null){
                    List<CheckItem> checkItems = new ArrayList<>();

                    for (Integer checkItemId : checkItemIds) {
                        //根据检查项id查询指定的检查项
                        CheckItem checkItem = checkItemDao.selectById(checkItemId);
                        //封装每个检查组的检查项
                        checkItems.add(checkItem);
                    }
                    checkGroup.setCheckItems(checkItems);
                }
                //封装该套餐的checkGroups集合
                checkGroups.add(checkGroup);
            }
            setmeal.setCheckGroups(checkGroups);
        }
        return setmeal;
    }


    /**
     * 查询套餐中有无使用对应的图片
     * @param photo
     * @return
     */
    @Override
    public Integer findCount(String photo) {
        Integer number=setMealDao.findNumber(photo);
        return number;
    }
}
